import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

typedef DurationChangedCallback = void Function(int second);

class BottomPicker extends StatefulWidget{

  BottomPicker({Key key,
    @required this.title, @required this.items,
    @required this.width, @required this.height,
    this.itemIndex=3,

    this.onDurationResetCallback,
  }) : super(key: key);

  final String title;
  final List<Text> items;
  final double width;
  final double height;
  final int itemIndex;

  final DurationChangedCallback onDurationResetCallback;

  @override
  State<StatefulWidget> createState() => _BottomPicker();

}


class _BottomPicker extends State<BottomPicker> {

  int duration = 0;

  @override
  Widget build(BuildContext context) {

    void onDurationReset(){
      if(this.duration == 0){
        this.duration = int.parse(widget.items[widget.itemIndex].data) * 60 + 1;
      }
      if(widget.onDurationResetCallback != null)
        widget.onDurationResetCallback(this.duration);
    }

    return Container(
      decoration: BoxDecoration(
          color: Colors.black,
          borderRadius: BorderRadius.all(Radius.circular(15))
      ),
      width: widget.width,
      height: widget.height,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [

          Container(
            width: widget.width,
            height: widget.height / 2,
            child:CupertinoPicker(
              backgroundColor: Colors.transparent,
              itemExtent: 25,
              squeeze: 1.3,
              scrollController: FixedExtentScrollController(initialItem: widget.itemIndex),
              selectionOverlay: PickerLineSelectionOverlay(),
              onSelectedItemChanged: (int index) {
                setState(() {
                  this.duration = int.parse(widget.items[index].data) * 60 + 1;
                });
              },
              children: widget.items,
            )
          ),

          CupertinoButton(
            child: Text('确定'),
            color: Colors.green,
            borderRadius: BorderRadius.all(Radius.circular(15)),
            onPressed: onDurationReset,
          ),
        ],
      )
    );
  }

}


class PickerLineSelectionOverlay extends StatelessWidget {

 
  PickerLineSelectionOverlay({
    Key key,
    this.capLeftEdge = true,
    this.capRightEdge = true,
  }) : assert(capLeftEdge != null),
        assert(capRightEdge != null),
        super(key: key);

  /// Whether to use the default use rounded corners and margin on the left side.
  final bool capLeftEdge;

  /// Whether to use the default use rounded corners and margin on the right side.
  final bool capRightEdge;

  /// Default margin of the 'SelectionOverlay'.
  final double _defaultSelectionOverlayHorizontalMargin = 0.16.sw;

  @override
  Widget build(BuildContext context) {

    return Container(
      margin: EdgeInsets.only(
        left: capLeftEdge ? _defaultSelectionOverlayHorizontalMargin : 0,
        right: capRightEdge ? _defaultSelectionOverlayHorizontalMargin : 0,
      ),
      decoration: BoxDecoration(
        border: Border(
            top: BorderSide(color: Colors.grey, width: 0.5),
            bottom: BorderSide(color: Colors.grey, width: 0.5),
            right: BorderSide.none,
            left: BorderSide.none,
        ),
      ),
      child: Row(

        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text('定时', style: TextStyle(fontSize: 60.sp, color: Colors.grey),),
          Text('分钟', style: TextStyle(fontSize: 60.sp, color: Colors.grey),),
        ],
      )
    );
  }
}


